package com.meihua.baoleyi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.bean.BankListResult;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.utils.CountDownTimerUtils;
import com.meihua.baoleyi.utils.ToastUtil;

import java.util.TreeMap;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 添加银行卡验证手机号
 * Created by liming on 2017/10/18
 */
public class AddBankVerifyPhoneActivity extends BaseActivity implements View.OnClickListener {

    private TextView add_bank_verify_phone_code_tv;//发送验证码
    private EditText add_bank_verify_phone_code_et;//验证码
    private TextView add_bank_verify_phone_finish;//完成
    private ImageView text_back;//返回
    private TextView text_title;//标题
    private String realName;
    private String banksCard;
    private String banksType;
    private String openPhone;
    private String bankId;


    private int time = 60;//倒计时

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //修改发送验证码文字
            if (msg.what == 0) {
                add_bank_verify_phone_code_tv.setText("发送验证码");
            } else {
                add_bank_verify_phone_code_tv.setText(msg.what + "S后重发");
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bank_verify_phone);

        initView();
        realName = getIntent().getStringExtra("real_name");//真实名称
        banksCard = getIntent().getStringExtra("banks_card");//银行卡
        banksType = getIntent().getStringExtra("banks_type");//银行类型
        openPhone = getIntent().getStringExtra("open_phone");//手机号
        bankId = getIntent().getStringExtra("bank_id");//银行id
        CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(AddBankVerifyPhoneActivity.this, add_bank_verify_phone_code_tv, 60000, 1000);
        countDownTimerUtils.start();
    }
    //初始化
    private void initView() {
        add_bank_verify_phone_code_tv = (TextView) findViewById(R.id.add_bank_verify_phone_code_tv);
        add_bank_verify_phone_code_et = (EditText) findViewById(R.id.add_bank_verify_phone_code_et);
        add_bank_verify_phone_finish = (TextView) findViewById(R.id.add_bank_verify_phone_finish);
        text_back = (ImageView) findViewById(R.id.text_back);
        text_title = (TextView) findViewById(R.id.text_title);

        text_title.setText("验证手机号");

        add_bank_verify_phone_code_tv.setOnClickListener(this);
        add_bank_verify_phone_code_et.addTextChangedListener(textWatcher);
        add_bank_verify_phone_finish.setOnClickListener(this);
        text_back.setOnClickListener(this);
    }

    /**
     * 验证码输入监听
     */
    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (add_bank_verify_phone_code_et.getText().toString().length() == 6) {
                add_bank_verify_phone_finish.setBackgroundResource(R.drawable.bank_next_bg_black);
                add_bank_verify_phone_finish.setEnabled(true);
            } else {
                add_bank_verify_phone_finish.setBackgroundResource(R.drawable.bank_next_bg_e0);
                add_bank_verify_phone_finish.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };
//添加点事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.text_back://返回
                finish();
                break;
            case R.id.add_bank_verify_phone_finish://完成
                if (add_bank_verify_phone_code_et.getText().toString().trim().isEmpty()) {
                    ToastUtil.show(AddBankVerifyPhoneActivity.this, "请输入验证码");
                    return;
                }
                TreeMap<String, String> treeMap = new TreeMap<>();
                treeMap.put("user_id", MyApp.getInstance().getUser_id());//用户id
                treeMap.put("real_name", realName);//真实名称
                treeMap.put("banks_card", banksCard);//银行卡号
                treeMap.put("bank_id", bankId);//银行id
                treeMap.put("phone", openPhone);//手机号
                treeMap.put("code", add_bank_verify_phone_code_et.getText().toString().trim());//验证码
                NetHelper.post(APIServer.SENDBANCODE,treeMap , true, new NetHelper.HttpCallBack() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        BankListResult bean = NetHelper.getBeanForJson(BankListResult.class, s);
                        if (bean == null) return;
                        if (bean.getCode().equals("200")) {
                            //请求成功
                            ToastUtil.show(AddBankVerifyPhoneActivity.this, bean.getMsg());
                            Intent in = new Intent();
                            in.putExtra("finish", 1);
                            setResult(1, in);
                            finish();
                        } else {
                            ToastUtil.show(AddBankVerifyPhoneActivity.this, bean.getMsg());
                        }
                    }
                });
                break;
            case R.id.add_bank_verify_phone_code_tv://发送验证码
                TreeMap<String, String> pa = new TreeMap<>();
                pa.put("phone", openPhone);//手机号
                NetHelper.get(APIServer.SENDREGISTER, pa, true, new NetHelper.HttpCallBack() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        BankListResult bean = NetHelper.getBeanForJson(BankListResult.class, s);
                        if (bean == null) return;
                        if (bean.getCode().equals("200")) {
                            //请求成功
                           Toast.makeText(AddBankVerifyPhoneActivity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(AddBankVerifyPhoneActivity.this, add_bank_verify_phone_code_tv, 60000, 1000);
                countDownTimerUtils.start();
                break;
        }
    }
}
